﻿// -----------------------------------------------------------------------
// <copyright file="CompressBoardConverter.cs" company="R&R">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

#region Imports
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
#endregion Imports

namespace Domino
{
    public class CompressBoardConverter : IValueConverter
    {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var board = value as ObservableCollection<TileModel>;
            var compressedBoard = new ObservableCollection<TileModel>();

            var splitCount = -1;
            if (!Int32.TryParse(System.Convert.ToString(parameter), out splitCount)) splitCount = 2;

            if (splitCount <= 0) return board;
            else if (board != null)
            {
                var front = board.Take(splitCount);
                var end = board.Reverse().Take(splitCount).Reverse();
                compressedBoard = new ObservableCollection<TileModel>(front.Union(end).Distinct());
                if (board.Count() > splitCount * 2) compressedBoard.Insert(splitCount, new TileModel(-1, -1));
            }

            return compressedBoard;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion IValueConverter Members
    }
}
